<?php
namespace Admin\Controller;

use Think\Controller;

class ExportExcelController extends Controller
{
    // function __destruct(){                                                                         // ��������规��  �����ョ�ㄦ�疯�块��������浣���ュ��
    //     require '/home/wwwroot/default/world/behavior.php';
    // }
    public function projectExportExcel(){
        
        header("Content-type: text/html; charset=utf-8");
        $Hint = new \Operation\Controller\HintController (); // 实例化Hint
        $Hint->checkLogin ( 3 ); // 验证是否登陆
        
        $project         = M('project');
        $projectcategory = M('projectcategory');
        
        $Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
        
        // 判断审核状态代码段
        if($_POST['adopt']=='on' && $_POST['not_through']==null){ $verify_pass = 'ischecked=1&&'; }
        if($_POST['adopt']==null && $_POST['not_through']=='on'){ $verify_pass = 'ischecked=2&&'; }
        if($_POST['adopt']=='on' && $_POST['not_through']=='on'){ $Hint->myerror("不会有这种情况：审核通过和审核未通过同时存在",'2'); die(); }
        // 判断已结项代码段
        if($_POST['already_Knot']=='on'){ $already_Knot = "enddate!='NULL'&&"; }
        // 判断招标否代码段
        if($_POST['bidding']=='on' && $_POST['stop_bidding']==null){  $whether_tender = 'isbid=1&&'; }
        if($_POST['bidding']==null && $_POST['stop_bidding']=='on'){  $whether_tender = 'isbid=2&&'; }
        if($_POST['bidding']=='on' && $_POST['stop_bidding']=='on'){ $Hint->myerror("不会有这种情况：即招标中和停止招标同时存在",'2'); die(); }
        // 项目类别代码段
        $projectcategory_string = "(";
        $projectcategory_count = $projectcategory->count();
        for($i = 1;$i<=$projectcategory_count;$i++){
            if($_POST['projectcategory'][$i]!=null){ $projectcategory_string = $projectcategory_string."cid=$i".'||'; }
        }
        if($projectcategory_string == "("){
            $projectcategory_string = null;
        }else{
            $projectcategory_string = substr($projectcategory_string,0,strlen($projectcategory_string)-2);
            $projectcategory_string = $projectcategory_string.')'.'&&';
        }
        
        // 负责人代码段
        if($_POST['leader']!=null){
            $leader = $_POST['leader'];
            $leader = 'leader='."'$leader'".'&&';
        }else{
            $leader = null;
        }
        
        // 时间段代码
        if($_POST['start_time']==null){ $start_time = "2016-01-01 00:00:00.000000"; }
        if($_POST['end_time']==null){ $end_time = date('Y-m-d H:i:s', time()); }
        if($_POST['start_time']!=null){
            $start_time = substr($_POST['start_time'],6,4).'-'.substr($_POST['start_time'],3,2).'-'.substr($_POST['start_time'],0,2);
        }
        if($_POST['end_time']!=null){
            $end_time   = substr($_POST['end_time'],6,4).'-'.substr($_POST['end_time'],3,2).'-'.substr($_POST['end_time'],0,2);
        }
        if($start_time>$end_time){ $Hint->myerror("不会有这种情况：起始时间大于结束时间！",'2'); die(); }
        $time = "adddate >='$start_time' && adddate <='$end_time'".'&&';
        // 各项值判断
        if($verify_pass            == null){ $verify_pass = ""; }
        if($whether_tender         == null){ $whether_tender = ""; }
        if($already_Knot           == null){ $already_Knot = ""; }
        if($projectcategory_string == null){ $projectcategory_string = ""; }
        if($leader                 == null){ $leader = ""; }
        if($time                   == null){ $time = ""; }
        // 处理字符串
        $select_string = $verify_pass.$already_Knot.$whether_tender.$projectcategory_string.$leader.$time;
        $select_string = substr($select_string,0,strlen($select_string)-2);
        $select_string = trim($select_string);
        
        $all_project_information = $Model->query("select *  from  project
                                                 left JOIN (
                                                            select  p_id.pid,  group_concat( users.stunum, users.realname, users.phone )
                                                            from  projectbid p_id,  users  where  p_id.uid = users.uid  and p_id.isjoined = 1  GROUP BY  p_id.pid )
                                                 bid on bid.pid = project.id
                                                 where $select_string
                                                 ");
        if(!empty($all_project_information)){
             derived_excel($all_project_information);
             die();
        }else{
             $Hint->myerror("没有相关数据",'3');
             die();
        }
         
        

    }
}